<?php
namespace app\common\models;


use think\Request;

class AdminUser extends BaseModel
{
    protected $auto = [];
    protected $insert = ['status' => 1, 'create_time'];
    protected $update = ['update_time'];


    public function getOneByLogin ($userName) {
        return $this->where(['username'=>$userName])->find();
    }
    protected function setCreateByAttr(){
        return $this->getAdminCreateBy();
    }
    protected function setCreateIpAttr(){
        return Request::instance()->ip();
    }
    protected function setPasswordAttr($password){
        return $this->setPassword($password);
    }

    public function setPassword($pwd){
        /**
         * setPassword
         *
         * @function password_hash() – 对密码加密.
         * @function password_verify() – 验证已经加密的密码，检验其hash字串是否一致.
         * @function password_needs_rehash() – 给密码重新加密.
         * @function password_get_info() – 返回加密算法的名称和一些相关信息.
         * @author: xujianping(814876569@qq.com)
         * @param $pwd
         * @return string
         */
        if ($pwd == ""){
            return $pwd;
        }
        return password_hash($pwd,PASSWORD_DEFAULT);
    }

    public function checkPassword($password,$hash){
        return password_verify($password,$hash);
    }

    public function updateLogin(AdminUser $userInfo,$data){
        return $userInfo->save($data);
    }
}